package com.game.message.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lizhiwei
 * @createAt Feb 17, 2015
 */
@Component
public class ProcessorManager {
	private static Logger logger = LoggerFactory.getLogger(ProcessorManager.class);
	@Autowired
	private ApplicationContext applicationContext;
	private Map<Integer, IProcessor> processorMap = new HashMap<Integer, IProcessor>();

	public IProcessor getProcess(int processorId) {
		return processorMap.get(processorId);
	}

	@PostConstruct
	public void register() {
		try {
			Map<String, IProcessor> processorBeanMap = this.applicationContext.getBeansOfType(IProcessor.class);
			for (Map.Entry<String, IProcessor> entry : processorBeanMap.entrySet()) {
				if (this.processorMap.containsKey(entry.getValue().getMsgId())) {
					logger.warn("allProcessor中发现重复消息id的Processor,消息id为" + entry.getValue().getMsgId());
					continue;
				}
				this.processorMap.put(entry.getValue().getMsgId(), entry.getValue());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
